package com.uzai.console.web.uzaichat;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.google.common.collect.Lists;
import com.uzai.common.enums.DeviceProductKeyEnum;
import com.uzai.common.enums.KefuSendMsgEnum;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.dto.QueryParam;
import com.uzai.console.dto.sysrole.SysRoleIdDto;
import com.uzai.console.dto.sysrole.SysRoleInfoDto;
import com.uzai.console.dto.sysrole.SysRoleQuery;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.sysrole.SysRoleService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.sysrole.SysRoleInfoVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "角色管理",tags = "客服管理|角色管理")
@ApiSort(23)
@RestController
public class SysRoleController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(SysRoleController.class);

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;

    @ApiOperation(value = "分页查询角色列表", notes = "分页查询角色列表", response = SysRoleInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/sysRole/findSysRoleList")
    public Object findSysRoleList(@RequestBody SysRoleQuery sysRoleQuery){
        return sysRoleService.findSysRoleList(sysRoleQuery);
    }

    @SystemLogType(model = SystemModelEnum.SysRole, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "添加角色", notes = "添加角色")
    @ApiOperationSupport(order = 2)
    @PostMapping("/sysRole/addSysRole")
    public Object addSysRole(@RequestBody SysRoleInfoDto sysRoleInfoDto){
        sysRoleService.addSysRole(sysRoleInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.SysRole, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "编辑角色", notes = "编辑角色" )
    @ApiOperationSupport(order = 3)
    @PostMapping("/sysRole/updateSysRole")
    public Object updateSysRole(@RequestBody SysRoleInfoDto sysRoleInfoDto){
        sysRoleService.updateSysRole(sysRoleInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.SysRole, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除", notes = "删除")
    @ApiOperationSupport(order = 4)
    @PostMapping("/sysRole/deleteSysRole")
    public Object deleteSysRole(@RequestBody SysRoleIdDto sysRoleIdDto){
        sysRoleService.deleteSysRole(sysRoleIdDto);
        return true;
    }

    @ApiOperation(value = "查询角色详情", notes = "查询角色详情", response = SysRoleInfoVo.class)
    @ApiOperationSupport(order = 5)
    @PostMapping("/sysRole/findSysRoleInfo")
    public Object findSysRoleInfo(@RequestBody SysRoleIdDto sysRoleIdDto){
        return sysRoleService.findSysRoleInfo(sysRoleIdDto);
    }

    @ApiOperation(value = "查询转发消息下拉列表", notes = "查询转发消息下拉列表",response = SelectKeyVo.class)
    @ApiOperationSupport(order = 6)
    @PostMapping("/sysRole/findSendMsgListSelect")
    public Object findSendMsgListSelect(){
        Long merId = getLoginInfo().getMerId();
        //悠聊功能开关包含的列表
        List<String> uzaichatList = Lists.newArrayList(
                KefuSendMsgEnum.ADDFRIEND.getKey(), KefuSendMsgEnum.FIRSTQUERY.getKey(), KefuSendMsgEnum.FIRSTQUERYREWARD.getKey(), KefuSendMsgEnum.QUERYSUCCESS.getKey(), KefuSendMsgEnum.QUERYERROR.getKey(), KefuSendMsgEnum.FIRSTORDER.getKey(),
                KefuSendMsgEnum.FIRSTORDERREWARD.getKey(), KefuSendMsgEnum.ORDERSUCCESS.getKey(), KefuSendMsgEnum.ORDERINVALID.getKey(),KefuSendMsgEnum.ORDERREFUND.getKey(), KefuSendMsgEnum.ORDERBINDERROR.getKey(), KefuSendMsgEnum.NOTPROMOT.getKey(),
                KefuSendMsgEnum.FIRSTTAKEBAL.getKey(), KefuSendMsgEnum.TAKEBALSUCCESS.getKey(), KefuSendMsgEnum.TAKEBALERROR.getKey(),KefuSendMsgEnum.TAKEBALCHECK.getKey(), KefuSendMsgEnum.SIGNINSUCCESS.getKey(), KefuSendMsgEnum.SIGNINERROR.getKey(),
                KefuSendMsgEnum.SHOPPINGGUIDE.getKey(),KefuSendMsgEnum.MDEXCHANGE.getKey(),KefuSendMsgEnum.CALLKEFU.getKey(),KefuSendMsgEnum.SUPERCMD.getKey(), KefuSendMsgEnum.OTHER_CMD.getKey(),KefuSendMsgEnum.UNSOLICITEDMSG.getKey(),
                KefuSendMsgEnum.UNKNOWMSG.getKey()
        );

        //返利功能开关包含的列表
        List<String> flList = Lists.newArrayList(
                KefuSendMsgEnum.FIRSTQUERY.getKey(), KefuSendMsgEnum.FIRSTQUERYREWARD.getKey(), KefuSendMsgEnum.QUERYSUCCESS.getKey(), KefuSendMsgEnum.QUERYERROR.getKey(), KefuSendMsgEnum.FIRSTORDER.getKey(),
                KefuSendMsgEnum.FIRSTORDERREWARD.getKey(), KefuSendMsgEnum.ORDERSUCCESS.getKey(), KefuSendMsgEnum.ORDERINVALID.getKey(),KefuSendMsgEnum.ORDERREFUND.getKey(), KefuSendMsgEnum.ORDERBINDERROR.getKey(), KefuSendMsgEnum.NOTPROMOT.getKey(),
                KefuSendMsgEnum.FIRSTTAKEBAL.getKey(), KefuSendMsgEnum.TAKEBALSUCCESS.getKey(), KefuSendMsgEnum.TAKEBALERROR.getKey(),KefuSendMsgEnum.TAKEBALCHECK.getKey(), KefuSendMsgEnum.SIGNINSUCCESS.getKey(), KefuSendMsgEnum.SIGNINERROR.getKey(),
                KefuSendMsgEnum.SHOPPINGGUIDE.getKey()
        );

        //免单功能开关包含的列表
        List<String> mdList = Lists.newArrayList(
                KefuSendMsgEnum.MDEXCHANGE.getKey()
        );

        List<SelectKeyVo> selectKeyVoList = new ArrayList<>();
        KefuSendMsgEnum[] enumArr = KefuSendMsgEnum.values();
        if(enumArr != null && enumArr.length > 0){
            for (KefuSendMsgEnum kefuSendMsgEnum : enumArr){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(kefuSendMsgEnum.getKey());
                selectKeyVo.setValue(kefuSendMsgEnum.getDesc());
                selectKeyVoList.add(selectKeyVo);
            }
        }

        //查询当前产品功能模块配置
        MerBaseConfig merBaseConfig_old = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.PRODUCT_SWITCH.getValue());
        JSONArray valArray = null;
        if(merBaseConfig_old != null) { //如果存在，则修改
            String val = merBaseConfig_old.getVal();
            if (StringUtils.isNotBlank(val)) {
                valArray = JSONObject.parseArray(val);
            }
        }

        if(valArray != null && valArray.size() > 0){
            //判断是否开启悠聊功能开关，如果不开启，则去掉悠聊相关的
            if(!valArray.contains(DeviceProductKeyEnum.uzaichat.getValue())){
                for (int i = selectKeyVoList.size()-1; i >= 0; i--) {
                    SelectKeyVo selectKeyVo = selectKeyVoList.get(i);
                    for (String key : uzaichatList) {
                        if(selectKeyVo.getKey().equals(key)){
                            selectKeyVoList.remove(i);
                            break;
                        }
                    }
                }
            }

            //判断是否开启返利功能开关，如果不开启，则去掉返利相关的
            if(!valArray.contains(DeviceProductKeyEnum.fl.getValue())){
                for (int i = selectKeyVoList.size()-1; i >= 0; i--) {
                    SelectKeyVo selectKeyVo = selectKeyVoList.get(i);
                    for (String key : flList) {
                        if(selectKeyVo.getKey().equals(key)){
                            selectKeyVoList.remove(i);
                            break;
                        }
                    }
                }
            }
            //判断是否开启免单功能开关，如果不开启，则去掉免单相关的
            if(!valArray.contains("md")){
                for (int i = selectKeyVoList.size()-1; i >= 0; i--) {
                    SelectKeyVo selectKeyVo = selectKeyVoList.get(i);
                    for (String key : mdList) {
                        if(selectKeyVo.getKey().equals(key)){
                            selectKeyVoList.remove(i);
                            break;
                        }
                    }
                }
            }
        }

        return selectKeyVoList;
    }

}
